
// controls object
local PANEL = { };


/*------------------------------------
	initialize controls
------------------------------------*/
function PANEL:Init( )

	// create media info area
	self.MediaInfo = vgui.Create( "MXMediaInfo" , self );

	// create seeking bar
	self.SeekBar = vgui.Create( "MXSeekBar", self );
	// move slider to start
	self.SeekBar:SetSlideX( 0 );
	// setup translate values function
	self.SeekBar.TranslateValues = function( self, x, y )
	
		// call our parents seek function
		self:GetParent():MoveSeekBar( x );
		
		// return the values
		return x, y;
	
	end

	// create shuffle button
	self.Shuffle = vgui.Create( "DImageButton", self );
	// tooltip
	self.Shuffle:SetTooltip( "#musex_controls_shuffle" );
	// set default image
	self.Shuffle:SetImage( "brick/brick_model" );
	// hook for skin to change
	MuseX.SkinHook( "ControlsShuffle", self.Shuffle, "SetImage" );
	// setup shuffle click command
	self.Shuffle.DoClick = function( self ) MuseX.ToggleShuffle(); end
	
	// create repeat button
	self.Repeat = vgui.Create( "DImageButton", self );
	// tooltip
	self.Repeat:SetTooltip( "#musex_controls_repeat" );
	// set default image
	self.Repeat:SetImage( "brick/brick_model" );
	// hook so skin can change it
	MuseX.SkinHook( "ControlsRepeat", self.Repeat, "SetImage" );
	// setup repeat click command
	self.Repeat.DoClick = function( self ) MuseX.ToggleRepeat(); end
	
	// create stop button
	self.Stop = vgui.Create( "DImageButton", self );
	// tooltip
	self.Stop:SetTooltip( "#musex_controls_stop" );
	// set the default image
	self.Stop:SetImage( "brick/brick_model" );
	// hook for skin changes
	MuseX.SkinHook( "ControlsStop", self.Stop, "SetImage" );
	// setup stop click command
	self.Stop.DoClick = function( self ) MuseX.Stop(); end
	
	// create previous button
	self.Previous = vgui.Create( "DImageButton", self );
	// tooltip
	self.Previous:SetTooltip( "#musex_controls_previous" );
	// set default image
	self.Previous:SetImage( "brick/brick_model" );
	// hook so skin can change
	MuseX.SkinHook( "ControlsPrevious", self.Previous, "SetImage" );
	// setup previous click command
	self.Previous.DoClick = function( self ) MuseX.PreviousSong( true ); end
	
	// create play/pause button
	self.PlayPause = vgui.Create( "DImageButton", self );
	// tooltip
	self.PlayPause:SetTooltip( "#musex_controls_playpause" );
	// set default image
	self.PlayPause:SetImage( "brick/brick_model" );
	// hook so skin can change
	MuseX.SkinHook( "ControlsPlay", self.PlayPause, "SetImage" );
	// setup play/pause click command
	self.PlayPause.DoClick = function( self )
		
		// make sure a song is loaded
		if ( !MuseX.SongLoaded() || ( !MuseX.IsPlaying() && !MuseX.IsPaused() ) ) then
		
			// get the currently selected song
			local song = MuseX.VGUI:GetSelectedPlaylistSong();
			
			// make sure we got one
			if ( !song ) then
			
				// get the first song
				song = MuseX.VGUI:GetPlaylistSong( 1 );
				
			end
			
			// load the song
			MuseX.LoadSong( song );
			
			// play the song
			MuseX.Play();
		
		else
		
			// play/pause
			MuseX.PlayPause();
		
		end
		
	end
	
	// create next button
	self.Next = vgui.Create( "DImageButton", self );
	// tooltip
	self.Next:SetTooltip( "#musex_controls_next" );
	// set default image
	self.Next:SetImage( "brick/brick_model" );
	// hook for skin
	MuseX.SkinHook( "ControlsNext", self.Next, "SetImage" );
	// setup next click command
	self.Next.DoClick = function( self ) MuseX.NextSong( true ); end
	
	// create mute button
	self.Mute = vgui.Create( "DImageButton", self );
	// tooltip
	self.Mute:SetTooltip( "#musex_controls_mute" );
	// set default image
	self.Mute:SetImage( "brick/brick_model" );
	// hook for skin
	MuseX.SkinHook( "ControlsMute", self.Mute, "SetImage" );
	// setup mute click command
	self.Mute.DoClick = function( self ) MuseX.ToggleMute(); end
	
	// create volume bar
	self.Volume = vgui.Create( "MXVolumeBar", self );
	// get initial position
	self:RefreshVolumePosition();
	// setup position value translation function
	self.Volume.TranslateValues = function( self, x, y )
	
		// set the volume
		MuseX.SetVolume( x );
		
		// return the values
		return x, y;
	
	end
	
	// create playlist info area
	self.PlaylistInfo = vgui.Create( "MXPlaylistInfo" , self );

end


/*------------------------------------
	layout the controls
------------------------------------*/
function PANEL:PerformLayout( )

	// let the BaseClass do its shit
	self.BaseClass.PerformLayout( self );
	
	// size & position the seekbar across the top
	self.SeekBar:SetSize( self:GetWide(), 20 );
	self.SeekBar:SetPos( 0, 0 );
	
	/* this shit goes from left to right */
	
	// Y axis position
	local ypos = 24;
	
	// validate the media info (compact mode destroys it)
	if ( self.MediaInfo && self.MediaInfo:IsValid() ) then
	
		// size & position the media info
		self.MediaInfo:SetSize( 150, 20 );
		self.MediaInfo:SetPos( ( self:GetWide() * 0.5 ) - 278, ypos );
		
	end
	
	// validate the shuffle button (compact mode destroys it)
	if ( self.Shuffle && self.Shuffle:IsValid() ) then
	
		// size & position shuffle button
		self.Shuffle:SetSize( 20, 20 );
		self.Shuffle:SetPos( ( self:GetWide() * 0.5 ) - 118, ypos );
		
	end
	
	// validate the repeat button (compact mode destroys it)
	if ( self.Repeat && self.Repeat:IsValid() ) then
	
		// size & position repeat button
		self.Repeat:SetSize( 20, 20 );
		self.Repeat:SetPos( ( self:GetWide() * 0.5 ) - 94, ypos );
		
	end
	
	// size & position stop button
	self.Stop:SetSize( 20, 20 );
	self.Stop:SetPos( ( self:GetWide() * 0.5 ) - 60, ypos );
	
	// size & position previous button
	self.Previous:SetSize( 20, 20 );
	self.Previous:SetPos( ( self:GetWide() * 0.5 ) - 36, ypos );
	
	// size & position play/pause button
	self.PlayPause:SetSize( 24, 24 );
	self.PlayPause:SetPos( ( self:GetWide() * 0.5 ) - 12, ypos - 2 );
	
	// size & position next button
	self.Next:SetSize( 20, 20 );
	self.Next:SetPos( ( self:GetWide() * 0.5 ) + 16, ypos );
	
	// size & position mute button
	self.Mute:SetSize( 20, 20 );
	self.Mute:SetPos( ( self:GetWide() * 0.5 ) + 40, ypos );
	
	// size & position volume bar
	self.Volume:SetSize( 60, 20 );
	self.Volume:SetPos( ( self:GetWide() * 0.5 ) + 74, ypos );
	
	// validate the playlist info (compact mode destroys it)
	if ( self.PlaylistInfo && self.PlaylistInfo:IsValid() ) then
	
		// size & position playlist info
		self.PlaylistInfo:SetSize( 140, 20 );
		self.PlaylistInfo:SetPos( ( self:GetWide() * 0.5 ) + 140, ypos );
		
	end
	
end


/*------------------------------------
	draw the panel
------------------------------------*/
function PANEL:Paint( )

	// use MuseX skin hooks
	derma.SkinHook( "Paint", "MXControls", self );
	
	// override
	return true;

end


/* CUSTOM PANEL FUNCTIONS */


/*------------------------------------
	seekbar has been moved
------------------------------------*/
function PANEL:MoveSeekBar( frac )

	// check if song is loaded
	if ( MuseX.SongLoaded() ) then
	
		// do clamping
		frac = math.Clamp( frac, 0, 1 );
		
		// seek to new position
		MuseX.Seek( frac );
		
	else
	
		// nothing loaded, put at begining
		self:SetSeekPosition( 0 );
	
	end
	
end


/*------------------------------------
	set the position of the seek bar
------------------------------------*/
function PANEL:SetSeekPosition( frac )

	// pass to seek bar
	self.SeekBar:SetPosition( frac );
	
end


/*------------------------------------
	set the media info table
------------------------------------*/
function PANEL:SetMediaInfo( tbl )

	// pass to media info object
	self.MediaInfo:SetMediaInfo( tbl );

end


/*------------------------------------
	show the playlist info
------------------------------------*/
function PANEL:ShowPlaylistInfo( text )

	// pass to playlist
	self.PlaylistInfo:ShowInfo( text );

end


/*------------------------------------
	refresh the volume bar position
------------------------------------*/
function PANEL:RefreshVolumePosition( )

	// set to current volume
	self.Volume:SetSlideX( MuseX.GetVolume() );

end

// define
derma.DefineControl( "MXControls", "", PANEL, "DPanel" );
